#!/bin/bash

if [ ! -x "@NM@" ];
then
  echo "Cannot check export format because nm is not installed" >&2
  exit 1
fi

DEFINED_ONLY="--defined-only"
EXTERN_ONLY="--extern-only"

if nm --help | grep -q llvm; then
    DEFINED_ONLY="-defined-only"
    EXTERN_ONLY="-extern-only"
fi

# this script outputs all symbols not starting with an allowed prefix
# exported symbols are allowed to start with
#    ts_      for regular timescaledb functions
#    pg_finfo for metadata defined by PG_FUNCTION_INFO_V1
# we also whitelist a couple of special symbols
#    _.*_init                     module startup functions
#    _.*_fini                     module shutdown functions
#    Pg_magic_func                used by postgres to check extension compatability
#    timescaledb_hello            used to test that our name collision resistance works
#    loader_hello                 used to test that our name collision resistance works
#    test_symbol_conflict         used to test that our name collision resistance works
#    tsl_license_update_check     used to check for valid license
# all of these symbols start with an additional leading '_' on macos
exports=$(find @CMAKE_BINARY_DIR@ -not -path '*/\.*' -name '*.so' -print0 \
    | xargs -0 @NM@ ${DEFINED_ONLY} ${EXTERN_ONLY} \
    | sed -e 's:^/.*$::' -e '/^$/d' -e 's/[a-f0-9]* [A-Za-z] //' \
    | grep -v \
        -e '^_\?ts_' \
        -e '^_\?tsl_license_update_check$' \
        -e '^_\?pg_finfo' \
        -e '^_\?_.*_init$' \
        -e '^_\?_.*_fini$' \
        -e '^_\?Pg_magic_func$' \
        -e '^_\?timescaledb_' \
        -e '^_\?loader_hello$' \
        -e '^_\?test_symbol_conflict$' \
)

num_exports=$(echo "${exports}" | grep -v '^$' | wc -l)
echo "${exports}"

if [ ${num_exports} -gt 0 ];
then
  exit 1
else
  exit 0
fi
